package com.vanysoft.business.system.dataDictionary.controller;

import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.business.system.dataDictionary.service.DataDictionaryService;
import com.vanysoft.common.utils.string.StringUtil;
import com.vanysoft.framework.base.action.BaseController;
import com.vanysoft.framework.config.mvc.SessionUser;
import com.vanysoft.framework.exception.VanySoftBaseException;
import com.vanysoft.framework.messagesource.MessageCodeConstant;
import com.vanysoft.framework.utils.JSONResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 类名称：DataDictionaryAction
 * 类描述：数据字典
 * 创建人：X
 * 创建时间：2017年9月24日上午10:31:12
 * 修改人：X
 * 修改时间：2017年9月24日
 * 版本信息：@version 1.0
 * Copyright (c) 2017SharpClaw-版权所有
 * 修改备注：
 */
@RestController
@RequestMapping("/system/dataDic")
public class DataDictionaryAction extends BaseController {


    @Autowired
    private DataDictionaryService dataDictionaryService;

    /**
     * Function Name               queryDataDic
     *
     * @return
     * @throws VanySoftBaseException
     * @description 获取数据字典列表数据
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年9月24日上午10:35:11          X          Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/queryDic", method = RequestMethod.GET)
    public JSONResponse queryDataDic(HttpServletRequest request) throws VanySoftBaseException {
        List<Map<String, Object>> list = dataDictionaryService.querySystemDataTable();
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, list);
    }

    /**
     * Function Name               queryTableConfiger
     *
     * @return
     * @throws VanySoftBaseException
     * @description 获取各字典表的字段详细信息
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年9月24日下午6:27:20          X          Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/queryConfig", method = RequestMethod.GET)
    public JSONResponse queryTableConfiger(HttpServletRequest request) throws VanySoftBaseException {
        List<Map<String, Object>> list = dataDictionaryService.queryTableConfiger();
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, list);
    }

    /**
     * Function Name               queryDictionaryList
     *
     * @return
     * @throws VanySoftBaseException
     * @description 根据传入代码表名称获取对应的代码列表
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年9月24日下午6:29:51          KING          Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/queryList", method = RequestMethod.GET)
    public JSONResponse queryDictionaryList(@SessionUser UserInfo userInfo, HttpServletRequest request, String scrollPageSize, String scrollPageIndex, String searchValue, String value1) throws VanySoftBaseException {
        Integer _pageIndex = StringUtil.isEmpty(scrollPageIndex) ? 1 : Integer.valueOf(scrollPageIndex);
        Integer _pageSize = StringUtil.isEmpty(scrollPageSize) ? 10 : Integer.valueOf(scrollPageSize);
        Map<String, Object> map = new HashMap<String, Object>();
        if (userInfo != null) {
            map = dataDictionaryService.queryDictionaryList(_pageSize, _pageIndex, searchValue, value1);
        }
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, map);
    }

    /**
     * Function Name               isRepeat
     *
     * @return
     * @throws VanySoftBaseException
     * @description 判断是否主键重复
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年9月24日下午7:09:41          KING          Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/isRepeat", method = RequestMethod.GET)
    public JSONResponse isRepeat(HttpServletRequest request, String tableCode, String field, String val) throws VanySoftBaseException {
        boolean isRepeat = dataDictionaryService.isRepeat(tableCode, field, val);
        Map map = new HashMap();
        map.put("isRepeat", isRepeat);
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, map);
    }

    /**
     * Function Name               editDic
     *
     * @param optType [1:add;2:update;3:delete]
     * @return
     * @throws VanySoftBaseException
     * @description 维护字典代码信息
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年10月11日下午11:46:05          X          Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/editDic", method = RequestMethod.GET)
    public JSONResponse editDic(@SessionUser UserInfo userInfo, HttpServletRequest request, String optType, String code, String name, String type, String description) throws VanySoftBaseException {
        if (userInfo != null) {
            dataDictionaryService.operaDictionary(optType, code, name, type, description);
        }
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS);
    }

    /**
     * Function Name               initDicHtmlJs
     *
     * @return
     * @throws VanySoftBaseException
     * @description 初始化时判断并生成html和JS
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年10月13日上午1:14:37          KING          Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/init", method = RequestMethod.GET)
    public JSONResponse initDicHtmlJs(HttpServletRequest request, String dicStr) throws VanySoftBaseException, IOException {
        Map map = new HashMap();
        boolean isCreate = dataDictionaryService.isCreate(dicStr);
        if (isCreate) {
            dataDictionaryService.createHtmlJs();
        }
        map.put("isCreate", isCreate);
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, map);
    }
}
